package com.mzj.saas.mservice.contract.mapper;

import com.mzj.saas.commons.DateUtils;
import com.mzj.saas.commons.util.ConvertUtils;
import com.mzj.saas.mservice.contract.vo.ContractListVO;
import org.springframework.jdbc.core.RowMapper;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ContractAppRowMapper implements RowMapper<ContractListVO> {
    @Override
    public ContractListVO mapRow(ResultSet rs, int rowNum) throws SQLException {
        ContractListVO vo = new ContractListVO();
        vo.setContractNubmer(rs.getString("contractNubmer"));
        vo.setHouseId(rs.getString("houseId"));
        vo.setOrgId(rs.getLong("orgId"));
        vo.setProvinceId(rs.getLong("provinceId"));
        vo.setCityId(rs.getLong("cityId"));
        vo.setDistrictId(rs.getLong("districtId"));
        vo.setStreetId(rs.getLong("streetId"));
        vo.setResidenceCommunityId(rs.getString("residenceCommunityId"));
        vo.setBuildingNum(rs.getLong("buildingNum"));
        vo.setUnit(rs.getLong("unit"));
        vo.setRoomNumber(rs.getString("roomNumber"));
        vo.setMatchUserId(rs.getString("matchUserId"));
        vo.setContractType(rs.getString("contractType") == null?null:Integer.parseInt(rs.getString("contractType")));
        Date startTime = rs.getDate("startTime");
        Date entTime = rs.getDate("entTime");
        vo.setStartTime(startTime == null ? "" : DateUtils.datetimeToString(startTime, DateUtils.FORMAT_DATE_8));
        vo.setEntTime(entTime == null ? "" : DateUtils.datetimeToString(entTime, DateUtils.FORMAT_DATE_8));

        Integer rentUnitAmount = 0;
        Integer rentAmount = 0;
        Integer payType = 0;
        Integer status = 0;
        if (rs.getObject("rentUnitAmount") != null) {
            rentUnitAmount = rs.getInt("rentUnitAmount");
        } else {
            rentUnitAmount = null;
        }

        if (rs.getObject("rentAmount") != null) {
            rentAmount = rs.getInt("rentAmount");
        } else {
            rentAmount = null;
        }

        if (rs.getObject("payType") != null) {
            payType = rs.getInt("payType");
        } else {
            payType = null;
        }

        if (rs.getObject("status") != null) {
            status = rs.getInt("status");
        } else {
            status = null;
        }

        vo.setRentUnitAmount(ConvertUtils.fenConvertYuan(rentUnitAmount));
        vo.setRentAmount(ConvertUtils.fenConvertYuan(rentAmount));
        vo.setPayType(payType);
        vo.setStatus(status);
        vo.setShortUrl(rs.getString("shortUrl"));
        vo.setContractDocUrl(rs.getString("contractDocUrl"));
        return vo;
    }
}
